/*
 * InvulgoTracker
 * Copyright (C) Thomas Andersen 2011 <thomas.1.andersen@gmail.com>
 *
 * InvulgoTracker is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * InvulgoTracker is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "taskid.h"
#include <string.h>
#include "config.h"
#include "gettextmacro.h"


static const gchar *key="ABCDEFGHIJKLMNOPQRSTUVWXY0123456789abcdefghijklmnopqrstuvwxy";
#define TASKIDSIZE 10

TaskId::TaskId()
{
    id = NULL;
}

TaskId::TaskId(const gchar *id)
{
    if (id && strcmp(id,"0")) {
        this->id = (gchar *)g_malloc(TASKIDSIZE+1);
        strcpy(this->id, id);
    } else
        this->id = NULL;
}

void TaskId::CopyData(const TaskId &ti)
{
    if (ti.id) {
        id = (gchar *)g_malloc(TASKIDSIZE+1);
        strcpy(id, ti.id);
    } else
        id = NULL;
}

TaskId::TaskId(const TaskId &ti)
{
    CopyData(ti);
}


TaskId::~TaskId()
{
    if (id)
        g_free(id);
}


void TaskId::Generate()
{
    id = (gchar *)g_malloc(TASKIDSIZE+1);
    memset(id,0,TASKIDSIZE+1);
    for(int i=0;i<TASKIDSIZE;++i) {
        id[i] = key[g_random_int_range(0,strlen(key))];
    }
}


TaskId& TaskId::operator=(const TaskId &rhs)
{
    if (this != &rhs) {
        if (this->id)
            g_free(this->id);
        CopyData(rhs);
    }
    return *this;
}


